/***************************************************************************
 *   Copyright (C) 2007 by Alexandre Masselot,,,   *
 *   alex@alex-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "runlcmsmsreadermgf.h"
namespace bol{
  boost::regex RunLcmsmsReaderMGF::s_re_charge("CHARGE=(.*)", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex RunLcmsmsReaderMGF::s_re_title("TITLE=(.*)", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex RunLcmsmsReaderMGF::s_re_pepmass("PEPMASS=(\\S+)\\s*(\\S*).*", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex RunLcmsmsReaderMGF::s_re_begin("BEGIN IONS.*", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex RunLcmsmsReaderMGF::s_re_end("END IONS.*", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex RunLcmsmsReaderMGF::s_re_peak("([\\d\\.]+)\\s*([\\d\\.]*).*", boost::regex_constants::perl);

  /**
   * 
   * @param run 
   * @param input 
   */
  void RunLcmsmsReaderMGF::init(RunLcmsms& run, istream* input){
    ios::pos_type pos=0;
    setInputStream(input);
    inputStream()->seekg(pos);
/*
//	dections of default can be done on-the-fly while reading
    string line;
    boost::cmatch matches;
    while(getline(*inputStream(), line)){
      // skip comments
      if(line.c_str()[0] == '#') continue;
      //first ions => exit init loop
      if(boost::regex_match(line, s_re_begin)){
        inputStream()->seekg(pos);
        break;
      }
      pos=inputStream()->tellg();

      // only consider defaults
      if(line.c_str()[0] != '@') continue;

      //default charge
      if(boost::regex_match(line.c_str()+1, matches, s_re_charge)){
        string s(matches[1]);
        setDefaultcharge(Peak::string2chargemask(s));
      }
    }
*/
  }

  SpectrumLcmsms* RunLcmsmsReaderMGF::nextSpectrum(RunLcmsms& run){
    SpectrumLcmsms *spectrum=NULL;
    double moz=0;
    Pchargemask chargemask=0;
    string description="";
    double intensity=0;

    string line;
    boost::cmatch matches;
    while(getline(*inputStream(), line)){
      if(line.c_str()[0] == '#')
        continue;

      // defaults overwrite ?
      if(line.c_str()[0] == '@') {
	
	//default charge
	if(boost::regex_match(line.c_str()+1, matches, s_re_charge)){
		string s(matches[1]);
		setDefaultcharge(Peak::string2chargemask(s));
	}
        continue;
      }
      
      //first ions => new spectrum
      if(boost::regex_match(line.c_str(), matches, s_re_begin)){
        //spectrum=new SpectrumLcmsms();
        continue;
      }

      //end store spectrum and returns
      if(boost::regex_match(line.c_str(), matches, s_re_end)){
        if(spectrum==NULL){
          //if the spectra contains no peak...
          spectrum=new SpectrumLcmsms(moz, chargemask, intensity);
          spectrum->setDescription(description);
        }

        if(!spectrum->chargemask()){
          spectrum->setChargemask(defaultcharge());
        }
        SpectrumPeakIntensityUtilities::computeIntensityRank(*spectrum);
        sort(spectrum->begin(), spectrum->end(), Peak::ltptr);
        run.msmsCollection().push_back(spectrum);
        return spectrum;
      }

      //charge
      if(boost::regex_match(line.c_str(), matches, s_re_charge)){
        string s(matches[1]);
        chargemask=Peak::string2chargemask(s);
        continue;
      }
      //title
      if(boost::regex_match(line.c_str(), matches, s_re_title)){
        string s(matches[1]);
        description=s;
        continue;
      }
       //precursor
      if(boost::regex_match(line.c_str(), matches, s_re_pepmass)){
        moz=boost::lexical_cast<double>(string(matches[1]));
        string s(matches[2]);
        if(s!=""){
          intensity=boost::lexical_cast<double>(s);
        }
        continue;
      }
       //peak
       ///@todo set charge info on fragmentation peaks
      if(boost::regex_match(line.c_str(), matches, s_re_peak)){
        if(spectrum==NULL){
          spectrum=new SpectrumLcmsms(moz, chargemask, intensity);
          spectrum->setDescription(description);
        }
        double pmoz=boost::lexical_cast<double>(string(matches[1]));
        string s(matches[2]);
        if(s!=""){
          spectrum->push_back(new PeakIntensity(pmoz, 0, boost::lexical_cast<double>(s)));
        }else{
          //@FIXME add a random intensity
          spectrum->push_back(new PeakIntensity(pmoz, 0, 0));
        }
        continue;
      }
    }
    return NULL;
  }
}
